package com.leyou.item.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.mapper.SpuMapper;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/5/5 0005
 * Time: 19:59
 */

@Service
@SuppressWarnings("all")
public class SkuServiceImpl implements SkuService {
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SpuDetailMapper spuDetailMapper;

    @Override
    public List<SkuDTO> querySkuBySpuId(Long id) {
        Sku sku = new Sku();
        sku.setSpuId(id);
        List<Sku> skuList = skuMapper.select(sku);
        if (CollectionUtils.isEmpty(skuList)) {
            throw new LyException(ExceptionEnum.GOODS_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(skuList, SkuDTO.class);
    }

    @Override
    public void addGoods(SpuDTO spuDTO) {
        Spu spu = BeanHelper.copyProperties(spuDTO, Spu.class);
        int insertSpu = spuMapper.insertSelective(spu);
        if (insertSpu != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //获取sku数组
        List<SkuDTO> skus = spuDTO.getSkus();
        //设置spu_id
        for (SkuDTO skuDTO : skus) {
            skuDTO.setSpuId(spu.getId());
        }
        List<Sku> skuList = BeanHelper.copyWithCollection(skus, Sku.class);
        int insertSkus = skuMapper.insertList(skuList);
        if (insertSkus != skuList.size()) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //获取spuDetail
        SpuDetailDTO spuDetailDTO = spuDTO.getSpuDetail();
        spuDetailDTO.setSpuId(spu.getId());
        int insertSpuDetail = spuDetailMapper.insertSelective(BeanHelper.copyProperties(spuDetailDTO, SpuDetail.class));
        if (insertSpuDetail != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    public void editGoods(SpuDTO spuDTO) {
        Spu spu = BeanHelper.copyProperties(spuDTO, Spu.class);
        int updateSpu = spuMapper.updateByPrimaryKeySelective(spu);
        if (updateSpu != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
        //如果要修改Sku先删所有sku
        //因为Sku每次都是新的组合表
        Sku skuDel = new Sku();
        skuDel.setSpuId(spu.getId());
        skuMapper.delete(skuDel);
        //获取sku数组 重新添加Sku
        List<SkuDTO> skus = spuDTO.getSkus();
        //设置spu_id
        for (SkuDTO skuDTO : skus) {
            skuDTO.setSpuId(spu.getId());
        }
        List<Sku> skuList = BeanHelper.copyWithCollection(skus, Sku.class);
        int insertSkus = skuMapper.insertList(skuList);
        if (insertSkus != skuList.size()) {
            //此处应抛更新
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
        int updateSpuDetail = spuDetailMapper.updateByPrimaryKeySelective(BeanHelper.copyProperties(spuDTO.getSpuDetail(), SpuDetail.class));
        if (updateSpuDetail != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    @Override
    public List<SkuDTO> querySkuByIds(List<Long> ids) {
        List<Sku> skus = skuMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(skus)) {
            throw new LyException(ExceptionEnum.GOODS_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(skus, SkuDTO.class);
    }

    @Override
    public void minusStock(Map<Long, Integer> cartMap) {
        for (Map.Entry<Long, Integer> entry : cartMap.entrySet()) {
            Long skuId = entry.getKey();
            Integer stock = entry.getValue();
            int i = skuMapper.minusStock(stock, skuId);
            if (i != 1) {
                throw new LyException(ExceptionEnum.SKU_STOCK_NOT_ENOUGH);
            }
        }
    }
}
